<template>
  <section class="toast-box">
    <transition-group name="toast">
      <div v-for="(toast, index) in toasts" :key="toast.timeoutId"  class="toast" :style="{backgroundColor: toastType[`${toast.type}`].color}">
        <div class="toast-content">
          <div class="toast-title">{{toast.title}}</div>
          <div class="toast-message">{{toast.message}}</div>
        </div>
        <div class="toast-close-button" v-on:click="close({index, timeoutId: toast.timeoutId})">×</div>
        <div class="toast-progress"></div>
      </div>
    </transition-group>
  </section>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import { TOAST_TYPE as toastType } from '../type'
export default {
  data () {
    return {
      toastType
    }
  },
  methods: {
    ...mapActions({
      close: 'toastRemove'
    })
  },
  computed: {
    ...mapState({
      toasts: state => state.toasts
    })
  }
}
</script>
<style scoped lang="less">
  .toast-box {
    position: fixed;
    z-index: 100;
    right: 15px;
    top: 15px;
    span {
      display: block;
    }
    .toast {
      position: relative;
      margin-bottom: 10px;
      width: 200px;
      border-radius: 5px;
      box-shadow: 0 0 3px #999;
      opacity: 0.9;
      padding: 15px;
      color: #FFF;
      display: flex;
      background: #F00;
      align-items: center;
    }
    .toast-content {
      width: 100%;
      word-wrap:break-word;
      .toast-title {
        font-weight: bold;
      }
    }

    .toast-close-button {
      position: absolute;
      top: 0;
      right: 0;
      width: 30px;
      text-align: center;
      height: 30px;
      line-height: 30px;
      font-size: 20px;
      color: #FFF;
      cursor: pointer;
      &:hover {
        color: #000;
        opacity: 0.4;
      }
    }
    .toast-progress {
      position: absolute;
      left: 0;
      bottom: 0;
      height: 4px;
      background-color: #000;
      opacity: 0.5;
      width: 0;
      transition-duration: 3s;
    }
  }
  .toast-enter-active {
    animation-name: fadeInRight;
    animation-duration: 0.5s;
  }
  .toast-leave-active {
    animation-name: fadeOut;
    animation-duration: 0.5s;
  }
  .toast-enter {
    .toast-progress {
      width: 100%;
    }
  }
  @keyframes fadeOut {
    0% {
      opacity: 1;
    }
    100% {
      opacity: 0;
    }
  }
  @keyframes fadeInRight {
    0% {
      opacity: 0;
      transform: translate3d(0, -100%, 0);
    }
    100% {
      opacity: 1;
      transform: translate3d(0, 0, 0);
    }
  }
</style>
